﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Helloworld.model
{
    class Animation
    {
        public Texture2D Image;
        public float FrameTime;
        public int TotalFrame;
        public int TotalFrameWidth;
        public float StateTime;
        public int width, height;
        public int xPos, yPos;

        public Animation(Texture2D image, float frametime, int totalFrame, int TotalFrameWidth, int x, int y)
        {
            this.Image = image;
            this.FrameTime = frametime;
            this.TotalFrame = totalFrame;
            
            this.xPos = x;
            this.yPos = y;
            this.StateTime = 0;
            this.TotalFrameWidth = TotalFrameWidth;
            this.width = this.Image.Width / this.TotalFrameWidth;
            this.height = this.Image.Height / (this.TotalFrame / this.TotalFrameWidth);
        }

        public void updateTime(float deltaltime)
        {
            StateTime += deltaltime;
        }
        public int getIndexFrame(float time,int []ArrayFrameIndex)
        {
            int index = (int)(time / this.FrameTime);
            index = index % ArrayFrameIndex.Count();
            return ArrayFrameIndex[index];
        }
        public void draw(SpriteBatch spriteBatch, int[] ArrayFrameIndex)
        {
            
            int index = getIndexFrame(this.StateTime, ArrayFrameIndex);
            drawFrameindex(spriteBatch,index);
        }

        public void drawFrameindex(SpriteBatch spriteBatch, int index)
        {
            int x = index % this.TotalFrameWidth;
            int y = index / this.TotalFrameWidth;
            int srcW = this.Image.Width / this.TotalFrameWidth;
            int srcH = this.Image.Height / (this.TotalFrame / this.TotalFrameWidth);
            spriteBatch.Draw(this.Image, new Rectangle(xPos, yPos, width, height), new Rectangle(x * srcW, y * srcH, srcW, srcH), Color.White);
        }

        public void setScale(float scale)
        {
            setScaleX(scale);
            setScaleY(scale);
        }
        public void setScaleY(float scale)
        {
            this.height *= (int)(this.height * scale);
        }
        public void setScaleX(float scale)
        {
            this.width = (int)(this.width * scale);
            
        }

    }
}
